package demo.huodongbaoming.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "activities")
@EqualsAndHashCode(exclude = {"creator", "participants", "category"})
@ToString(exclude = {"creator", "participants", "category"})
public class Activity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String title;

    @Column(length = 2000)
    private String description;

    @Column(name = "start_time")
    private LocalDateTime startTime;

    @Column(name = "end_time")
    private LocalDateTime endTime;

    @Column(name = "registration_deadline")
    private LocalDateTime registrationDeadline;

    private String location;

    @Column(name = "max_participants")
    private Integer maxParticipants;

    @Column(name = "current_participants")
    private Integer currentParticipants = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "creator_id")
    private User creator;

    @ManyToMany(mappedBy = "registeredActivities", fetch = FetchType.LAZY)
    private Set<User> participants = new HashSet<>();
    
    @Column(name = "is_active")
    private Boolean isActive = true;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id")
    private ActivityCategory category;
    
    @Column(name = "cover_image")
    private String coverImage;
    
    @Column(name = "sign_in_code")
    private String signInCode;
    
    @Column(name = "is_featured")
    private Boolean isFeatured = false;
    
    @Column(name = "created_at")
    private LocalDateTime createdAt;
    
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;
    
    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
        updatedAt = LocalDateTime.now();
    }
    
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }
    
    public void setActive(boolean active) {
        this.isActive = active;
    }
    
    // 显式添加setter和getter方法，以防lombok注解不起作用
    public void setCreator(User creator) {
        this.creator = creator;
    }
    
    public User getCreator() {
        return this.creator;
    }
    
    public void setCategory(ActivityCategory category) {
        this.category = category;
    }
    
    public ActivityCategory getCategory() {
        return this.category;
    }
}